<?php

Loader::import('pspframework.datasource.DataSource');
Loader::import('pspframework.datasource.DboSource');
Loader::import('config.database');


class ConnectionManager{


	public static $config = null;


	public static $_dataSources = array();


	public static $_connectionsEnum = array();

	protected static $_init = false;	
	
	public static function init() {
		if (empty(self::$config) &&  class_exists('DataBaseConfig')) {	
			self::$config =& new DataBaseConfig();
			self::_getConnectionObjects();
		}		
        self::$_init = true; 	
		
	}

	public static function _getConnectionObjects() {
		if (empty(self::$_init)) {
			self::init();
		}	      
		$connections = get_object_vars(self::$config);
		if ($connections != null) {
			foreach ($connections as $name => $config) {
				self::$_connectionsEnum[$name] = self::__connectionData($config);
			}
		}
	}

	public static function __connectionData($config) {
		if (!isset($config['datasource'])) {
			return false;
		}
        $classname = 'Dbo' . $config['datasource'];	
        $filename = $config['datasource'];		
		return compact('classname','filename');
	}
	
	

	public static function &getDataSource($name) {
		if (empty(self::$_init)) {
			self::init();
		}
		if (!empty(self::$_dataSources[$name])) {
			$return =& self::$_dataSources[$name];
			return $return;
		}
		if (empty(self::$_connectionsEnum[$name])) {
			trigger_error(sprintf("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR);
			$null = null;
			return $null;
		}
		$conn = self::$_connectionsEnum[$name];
		$class = $conn['classname'];

		if (self::loadDataSource($name) === null) {
			trigger_error(sprintf("ConnectionManager::getDataSource - Could not load class %s", $class), E_USER_ERROR);
			$null = null;
			return $null;
		}		
		self::$_dataSources[$name] =& new $class(self::$config->{$name});
		self::$_dataSources[$name]->configKeyName = $name;

		$return =& self::$_dataSources[$name];
		return $return;
	}

	public static function loadDataSource($connName) {
		if (empty(self::$_init)) {
			self::init();
		}
		if (is_array($connName)) {
			$conn = $connName;
		} else {
			$conn = self::$_connectionsEnum[$connName];
		}

		if (class_exists($conn['classname'])) {
			return false;
		}
		$class = $conn['classname'];

		if (!Loader::import('datasource.'.$conn['filename'])) {
			trigger_error(sprintf('ConnectionManager::loadDataSource - Unable to import DataSource class %s', $class), E_USER_ERROR);
			return null;
		}
		return true;
	}	

	public static function sourceList() {
		if (empty(self::$_init)) {
			self::init();
		}
		return array_keys(self::$_dataSources);
	}


	public static function getSourceName(&$source) {
		if (empty(self::$_init)) {
			self::init();
		}
		foreach (self::$_dataSources as $name => $ds) {
			if ($ds == $source) {
				return $name;
			}
		}
		return '';
	}



	public static function enumConnectionObjects() {
		if (empty(self::$_init)) {
			self::init();
		}
		return (array)self::$config;
	}


	public static function create($name = '', $config = array()) {
		if (empty(self::$_init)) {
			self::init();
		}

		if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
			return null;
		}
		self::$config->{$name} = $config;
		self::$_connectionsEnum[$name] = self::__connectionData($config);
		$return = self::getDataSource($name);
		return $return;
	}


	public static function drop($name) {
		if (empty(self::$init)) {
			self::init();
		}
		if (!isset(self::$config->{$name})) {
			return false;
		}
		unset(self::$_connectionsEnum[$name], self::$_dataSources[$name], self::$config->{$name});
		return true;
	}
}
